// 添加在线图层方法
function addOnlineLayer(fun) {
    /////////////////////////
    // 天地图
    var TianDiTu = new Cesium.TiandituImageryProvider({});
// 全球中文注记服务(经纬度)
    var global_CH_Service_XY = new Cesium.TiandituImageryProvider({
        mapStyle: Cesium.TiandituMapsStyle.CIA_C, //天地图全球中文注记服务（经纬度投影）
        token: URL_CONFIG.TOKEN_TIANDITU
    });
// 全球影像地图服务(经纬度)
    var global_IMG_Service_XY = new Cesium.TiandituImageryProvider({
        mapStyle: Cesium.TiandituMapsStyle.IMG_C, //天地图全球中文注记服务（经纬度投影）
        token: URL_CONFIG.TOKEN_TIANDITU
    });
// 全球矢量地图服务(墨卡托)
    var global_SHP_Service_MKT = new Cesium.TiandituImageryProvider({
        mapStyle: Cesium.TiandituMapsStyle.VEC_W, //天地图全球中文注记服务（经纬度投影）
        token: URL_CONFIG.TOKEN_TIANDITU
    });
// 全球地形晕染服务(墨卡托)
    var global_DEM_Service_MKT = new Cesium.TiandituImageryProvider({
        mapStyle: Cesium.TiandituMapsStyle.TER_W, //天地图全球中文注记服务（经纬度投影）
        token: URL_CONFIG.TOKEN_TIANDITU
    });
// 全球影像地图服务(墨卡托)
    var global_IMG_Service_MKT = new Cesium.TiandituImageryProvider({
        mapStyle: Cesium.TiandituMapsStyle.IMG_W, //天地图全球中文注记服务（经纬度投影）
        token: URL_CONFIG.TOKEN_TIANDITU
    });
// 全球矢量地图服务(经纬度)
    var global_SHP_Service_MKT = new Cesium.TiandituImageryProvider({
        mapStyle: Cesium.TiandituMapsStyle.VEC_C, //天地图全球中文注记服务（经纬度投影）
        token: URL_CONFIG.TOKEN_TIANDITU
    });
// 全球地形晕染服务(经纬度)
    var global_DEM_Service_XY = new Cesium.TiandituImageryProvider({
        mapStyle: Cesium.TiandituMapsStyle.TER_C, //天地图全球中文注记服务（经纬度投影）
        token: URL_CONFIG.TOKEN_TIANDITU
    });
// BingMap底图
    var BingMap = new Cesium.BingMapsImageryProvider({
        key: URL_CONFIG.BING_MAP_KEY,//可至官网（https://www.bingmapsportal.com/）申请key
        url: URL_CONFIG.BINGMAP
    });
// ArcGISService底图
    var ArcGISService = new Cesium.CGCS2000MapServerImageryProvider({
        url: 'http://cache1.arcgisonline.cn/arcgis/rest/services/ChinaOnlineStreetPurplishBlue/MapServer'
        // suggest: true,//4490坐标系，按照suggest切片方案切的瓦片,需要设置该参数
    });
// ArcGISService全球剖分层级格网
    var tile_lay = new Cesium.TileCoordinatesImageryProvider({});
// 本地图片
    var localPic = new Cesium.SingleTileImageryProvider({
        url: '../../img/worldimage.jpg'
    });
    /////////////////////////
    viewer.imageryLayers.addImageryProvider(
        eval(fun)
    );

    image_layers_index.push(fun);
}


// 添加自定义图层方法
function addCustomLayer(name, type, url) {
    switch (type) {
        // 矢量
        case "S" :
            shpOpen(name, url);
            break;

        // 影像
        case "T" :
            tifOpen(name, url);
            break;

        // 倾斜摄影
        case "O" :
            osgbOpen(name, url);
            break;

        // 地形
        case "D" :
            demOpen(name, url);
            break;
    }
}

// 矢量图层加载方法
function shpOpen(name, url) {
    if (url == null || url == '' || url == undefined) {
        return false;
    } else {
            if(url.indexOf('vectortile/maps')>-1){//mvt
                mvt(name, url);
            }else if(url.indexOf('rest/maps')>-1){//栅格
                addImageryProvider(name, url);
            }else if(url.indexOf('rest/realspace/datas')>-1){//添加指定单个的scene图层
                addS3MTilesLayerByScp(name, url);
            }else if(url.indexOf('rest/realspace')>-1)//open 所有scene图层
                addScene(name, url);

    }
}

// shp图层四种
function mvt(name, url){
    var mvtMap;
    mvtMap = viewer.scene.addVectorTilesMap({
        url: url,
        name: name,
        viewer: viewer
    });
}

// shp图层四种
function addImageryProvider(name, url){
    var imageryLayers = viewer.imageryLayers;
    //依次利用图片服务url创建SuperMapImageryProvider实例
    var img = new Cesium.SuperMapImageryProvider({
        url : url,
        name:name
//      url : "http://localhost:9000/iserver/services/3D-XingAnMengYingXiang/rest/realspace/datas/%E9%81%A5%E6%84%9F%E5%BD%B1%E5%83%8F@%E5%BC%80%E5%8F%91"
    });
    var layer = imageryLayers.addImageryProvider(img,1);
    layer.transperantBackColor= Cesium.Color.fromCssColorString('#333');
    layer.transparentBackColorTolerance = Number(0.1); //去黑边
    image_layers_index.push(name);
    image_layers_index.push(name);
    //view.flyTo(layer);
    all_layers_put(name,name, url, 'image', []);
}

// shp图层四种
function addS3MTilesLayerByScp(name, url){
    if(scene == undefined)
        scene = viewer.scene;
    if(fromDegrees == undefined){
        fromDegrees = Cesium.Cartesian3.fromDegrees(122.044427730947 ,46.072321618353 ,589.33)
    }

    if(orientation == undefined){
        orientation = {
            heading : Cesium.Math.toRadians(0), // 方向
            pitch : Cesium.Math.toRadians(0),// 倾斜角度
            roll : 0
        }
    }
    orientation = {
        heading : Cesium.Math.toRadians(-45), // 方向
        pitch : Cesium.Math.toRadians(-60),// 倾斜角度
        roll : 0
    };
    /*Heading:初始方向为正北，正角度为向东旋转，即水平旋转，也叫偏航角
    Pitch:正角度为平面之上，负角度为平面之下，即前后旋转，也叫俯仰角
    Roll:左右旋转，也叫翻滚角*/
    var widget = viewer.cesiumWidget;
    try{
        //添加S3M图层
        var promise = scene.addS3MTilesLayerByScp(url,{name : name});
        var promiseSet = [promise];
        Cesium.when.all(promiseSet,function(layers){
            for(var i=0;i<layers.length;i++){
                layers[i].selectEnabled = false;
                //layers[i].shadowType = 2;//设置图层的阴影模式
                var layer_obj_child = {};
                layer_obj_child.name = layers[i].name;
                layer_obj_child.layer_type = 'scene';
                scene_layers_index.push(layer_obj_child);
            }
        },function(e){
            if (widget._showRenderLoopErrors) {
                var title = '加载SCP失败，请检查网络连接状态或者url地址是否正确？';
                widget.showErrorPanel(title, undefined, e);
            }
        });
        //   view.flyTo(promise);
    }
    catch(e){
        if (widget._showRenderLoopErrors) {
            var title = '渲染时发生错误，已停止渲染。';
            widget.showErrorPanel(title, undefined, e);
        }
    }
    if(fromDegrees !=undefined && orientation !=undefined){
        viewer.camera.flyTo({
            destination : fromDegrees,
            orientation :  orientation,
            duration:9
        });
    }
    all_layers_put(name,name, url, 'scene', []);
}

// shp图层四种
function addScene(url,name){

    var layer_obj_childs = [];

    var promise = viewer.scene.open(url);
    Cesium.when(promise,function(layers){
        for(var i=0;i<layers.length;i++){
            console.log(layers[i].name);
            if(layers[i].name.indexOf("行政区")>-1){

                if(layers[i].name.indexOf("行政区2000@行政区")>-1){
                    layers[i].selectEnabled = false;
                    layers[i].visible = false;
                }
                if(layers[i].name.indexOf("行政区#")>-1){
                    layers[i].selectEnabled = true;
                    layers[i].visible = true;


                }
                if(layers[i].name.indexOf("界线2000@行政区")>-1){
                    layers[i].selectEnabled = false;
                    layers[i].visible = true;
                    //layers[i].selectedColor = Cesium.Color.CHARTREUSE;
                    //layers[i].visible = true;
                    if(userxzqh =="152200"){
                        layers[i].visible = true;
                    }else
                        layers[i].visible = true;
                }

            }else{
                layers[i].selectEnabled = true;
                layers[i].visible = true;
            }

            var queryPoint = { // 查询点对象
                x: x,
                y: y
            };
            var position1 = scene.camera.position;
            //将笛卡尔坐标转化为经纬度坐标
            var cartographic = Cesium.Cartographic.fromCartesian(position1);

            height1 = cartographic.height;
            // ["行政区:村级行政区2000","行政区:镇级行政区2000","行政区:旗县级行政区2000"]
            if(height1> 108054.5569){
                datanames =  ["行政区:旗县级行政区2000"];
                cxUrl = "旗县级行政区界线2000@行政区";
                //xzColor = Cesium.Color.TOMATO ;
            }else if(height1<108054.5569 && height1> 29931.9214){
                datanames =  ["行政区:镇级行政区界线2000"];
                cxUrl = "镇级行政区界线2000@行政区";
                //xzColor = "rgb(0, 34, 204)";
            }else if( height1< 29931.9214){
                datanames =  ["行政区:村级行政区2000"];
                cxUrl = "村级行政区界线2000@行政区";
                //xzColor = "rgb(170, 255, 0)" ;
            }

            if(i ==0 && userxzqh != '152200'){
                // queryByPoint(queryPoint);
            }

            var layer_obj_child = {};
            layer_obj_child.name = layers[i].name;
            layer_obj_child.layer_type = 'scene';
            layer_obj_childs.push(layer_obj_child);
            scene_layers_index.push(layer_obj_child);
        }

        all_layers_put(name,name, url, 'scene', layer_obj_childs);
    });
}



function all_layers_put(tname, name, url, layer_type, layer_obj_childs) {
    var layer_obj = {};
    layer_obj.name = name;
    layer_obj.tname = tname;
    layer_obj.url = url;
    layer_obj.layer_type = layer_type;
    layer_obj.layer_obj_childs = layer_obj_childs;
    all_layers.push(layer_obj);
}


// 影像图层加载方法
function tifOpen(name, url) {
    if (url == null || url == '' || url == undefined) {
        return false;
    } else {
            viewer.imageryLayers.addImageryProvider(new Cesium.SuperMapImageryProvider({
                url: url,
                name: name,
                invisibility: true,
                visible: true
            }));


    }
}

// 倾斜摄影图层加载方法
function osgbOpen(name, url) {
    if (url == null || url == '' || url == undefined) {
        return false;
    } else {
        var scene = viewer.scene;
            var promise = scene.open(url, undefined, {
                autoSetView: false      //不自动定位
            });
            Cesium.when(promise, function () {
                var title = '加载SCP失败，请检查网络连接状态或者url地址是否正确？';
                widget.showErrorPanel(title, undefined, e);
            });

    }


}

// 地形图层加载方法
function demOpen(name, url) {
    if (url == null || url == '' || url == undefined) {
        return false;
    } else {
            viewer.terrainProvider = new Cesium.CesiumTerrainProvider({
                url: url,
                name: name
            });

    }
}